package com.woniuxy.cloud.userassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.entity.TGoodCar;
import com.woniuxy.cloud.form.AddGoodCarForm;
import com.woniuxy.cloud.form.AddUserCarForm;
import com.woniuxy.cloud.userassembly.mapper.TGoodCarMapper;
import com.woniuxy.cloud.userassembly.service.ITGoodCarService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品购物车表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-24
 */
@Service
public class TGoodCarServiceImpl extends ServiceImpl<TGoodCarMapper, TGoodCar> implements ITGoodCarService {

    /**
     * 根据商品购物车id获取商品购物车
     * @param goodCarId 商品购物车id
     * @return TGoodCar
     */
    @Override
    public TGoodCar getGoodCarByGoodCarId(Integer goodCarId) {
        return  getBaseMapper().selectById(goodCarId);
    }

    /**
     * 查询商品购物车是否重复
     * @param userId 用户id
     * @param goodCarStoreGoodId 商品id
     * @return TGoodCar
     */
    @Override
    public TGoodCar getGoodCarByUserIdAndGoodId(Integer userId,Integer goodCarStoreGoodId){
        QueryWrapper<TGoodCar> wrapper = new QueryWrapper<>();
        wrapper.eq("good_car_user_id",userId).eq("good_car_store_good_id",goodCarStoreGoodId);
        return getBaseMapper().selectOne(wrapper);
    }


    /**
     * 新增购物车
     * @param tGoodCar
     * @return
     */
    @Override
    public Integer addGoodCar(TGoodCar tGoodCar) {
     return getBaseMapper().insert(tGoodCar);
    }

    /**
     * 删除购物车
     * @param GoodCarId
     * @return
     */
    @Override
    public Integer deleteUserGoodCarByGoodCarId(Integer GoodCarId) {
        return getBaseMapper().deleteById(GoodCarId);
    }

    /**
     * 修改购物车数量并修改小计
     * @param goodCarId
     * @param goodCarNum
     * @return
     */
    @Override
    public Integer updateGoodCarNumByGoodCarId(Integer goodCarId, Integer goodCarNum,Double total) {
        UpdateWrapper<TGoodCar> wrapper = new UpdateWrapper<>();
        wrapper.eq("good_car_id",goodCarId).set("good_car_num",goodCarNum).set("good_car_total",total);
        return getBaseMapper().update(null,wrapper);
    }



    /**
     * 获取用户购物车列表
     * @param userId
     * @return
     */
    @Override
    public List<TGoodCar> getGoodCarListByUserId(Integer userId) {
        QueryWrapper<TGoodCar>wrapper = new QueryWrapper<>();
        wrapper.eq("good_car_user_id",userId);
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    public List<TGoodCar> getGoodCarByIds(List<Integer> ids) {
        UpdateWrapper<TGoodCar> tGoodCarUpdateWrapper = new UpdateWrapper<>();
        tGoodCarUpdateWrapper.in("good_car_id", ids);
        List<TGoodCar> tGoodCars = getBaseMapper().selectList(tGoodCarUpdateWrapper);
        return tGoodCars;
    }


}
